//
//  MovieViewController.m
//  豆瓣电影
//
//  Created by dllo on 16/6/23.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "MovieViewController.h"
#import "MovieModel.h"
#import "MovieCollectionController.h"
#import "CustomTableViewCell.h"
#import "MovieDetailViewController.h"

@interface MovieViewController ()<UITableViewDataSource,UITableViewDelegate>

@property (nonatomic, retain) NSMutableArray *movieArray;

@property (nonatomic, retain) UITableView *tableView;



@end

@implementation MovieViewController

-(void)dealloc
{
    [_tableView release];
    [_movieArray release];
    [super dealloc];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self config];
    
    [self handleData];
    
    [self createTableView];
    
    [self createNavigationItem];
    
}

/**视图将要出现*/
-(void)viewWillAppear:(BOOL)animated
{
    self.navigationController.navigationBar.barTintColor = [UIColor colorWithRed:0.41 green:0.61 blue:0.68 alpha:1.0];
}

-(void)config
{
    self.movieArray = [NSMutableArray array];
    
    self.navigationItem.title = @"电影";
    
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    self.view.backgroundColor = [UIColor whiteColor];
    
}

#pragma mark - 解析数据
-(void)handleData
{
    NSString *path = [[NSBundle mainBundle] pathForResource:@"movielist" ofType:@"txt"];
    
    NSData *data = [NSData dataWithContentsOfFile:path];
    
    NSError *error = nil;
    
    NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:0 error:&error];
    
    NSArray *array = [dic objectForKey:@"result"];
    
    for (NSDictionary *dic in array) {
        MovieModel *movie = [[MovieModel alloc]init];
        
        [movie setValuesForKeysWithDictionary:dic];
        
        [self.movieArray addObject:movie];
    }
    //    NSLog(@"%@", self.movieArray);
}

-(void)createNavigationItem
{
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"btn_nav_collection@2x.png"] style:UIBarButtonItemStylePlain target:self action:@selector(movieCollectionAction:)];
}

-(void)movieCollectionAction:(UINavigationItem *)item
{
    MovieCollectionController *movieCollectionVC = [[MovieCollectionController alloc]init];
    
    [self.navigationController pushViewController:movieCollectionVC animated:YES];
}

#pragma mark - tableView
-(void)createTableView
{
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 64, CGRectGetWidth(self.view.frame), CGRectGetHeight(self.view.frame) - 105) style:UITableViewStylePlain];
    
    [self.view addSubview:self.tableView];
    
    self.tableView.dataSource = self;
    
    self.tableView.delegate = self;
    
    self.tableView.rowHeight = 100;
    
    [self.tableView registerClass:[CustomTableViewCell class] forCellReuseIdentifier:@"cell"];
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.movieArray.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    CustomTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    
    cell.cellModel = [self.movieArray objectAtIndex:indexPath.row];
    
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    MovieModel *movieModel = [self.movieArray objectAtIndex:indexPath.row];
    
    MovieDetailViewController *detailVC = [[MovieDetailViewController alloc]init];
    
    detailVC.idText = movieModel.movieId;
    
    detailVC.nameText = movieModel.movieName;
    
    [self.navigationController pushViewController:detailVC animated:YES];
    
}

@end























